/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se392.ateam2006.resourcemgmt.participant.exclusionset;

import java.util.ArrayList;
import java.util.Collection;
import se392.ateam2006.meetingmgmt.meeting.MeetingEntity;
import se392.ateam2006.resourcemgmt.ResourceMgmtLocal;
import se392.ateam2006.resourcemgmt.participant.ParticipationEntity;
import se392.ateam2006.utils.Debug;

/**
 * Non-Persistent class for intermediate management of Exclusion Sets;
 * @author rob
 */
public class TransientExclusionSetManager {

    ArrayList<ExclusionRangeEntity> loadedSet;
    ResourceMgmtLocal rmb;

    public TransientExclusionSetManager(Collection<ExclusionRangeEntity> exSet, ResourceMgmtLocal rmb) {
        loadedSet = new ArrayList<ExclusionRangeEntity>();
        loadedSet.addAll(exSet);
        this.rmb = rmb;
    }

    public ArrayList<ExclusionRangeEntity> getExSetsForActiveParticipants(MeetingEntity m) {
        return this.getSubset(m, true, false);
    }

    public ArrayList<ExclusionRangeEntity> getPrefSetsForActiveParticipants(MeetingEntity m) {
        return this.getSubset(m, true, true);
    }

    public ArrayList<ExclusionRangeEntity> getExSetsForPassiveParticipants(MeetingEntity m) {
        return this.getSubset(m, false, false);
    }

    public ArrayList<ExclusionRangeEntity> getPrefSetsForPassiveParticipants(MeetingEntity m) {
        return this.getSubset(m, false, true);
    }

    //Called by the functions above, this simply returns a nice subset.
    protected ArrayList<ExclusionRangeEntity> getSubset(MeetingEntity m, boolean isActive, boolean isPreference) {
        Debug.print(1,"getSubset starts");
        ArrayList<ExclusionRangeEntity> localList = new ArrayList<ExclusionRangeEntity>();
        ParticipationEntity p;
        for (ExclusionRangeEntity e : this.loadedSet) {
            // if(e==null) DebugHelper.print("TSM : getSubset : " + "e is null");
            //if(e.getUserEnt()==null) DebugHelper.print("TSM : getSubset : " + "e.getUserEnt() is null");
            //if(e.getUserEnt().getPet()==null) DebugHelper.print("TSM : getSubset : " + "e.getUserEnt().getPet() is null");
            p = rmb.getParticipant(e.getUserEnt(), m);
            if (p == null) {
                Debug.print("TSM : getSubset : p is null!");
                return null;
            }

            //        (e.getUserEnt(),m);
            //    rmbean.getParticipant(user,meeting)
            if (p.isActive() != isActive) {
                continue;
            }
            if (e.getPreferenceSet() != isPreference) {
                continue;
            }

            if (e.getStartDate() == null || e.getEndDate() == null) {
                Debug.print(9, "getSubset : Invalid Exlusion set - date ranges not set!");
            } else {
                localList.add(e);
            }
        }

        //Need to sort localList before return ?
        //Possibly, At the moment it "should" be in order as returned by the original jpa

        return localList;
    }
}
